var [username, password] = document.getElementsByTagName('input')
var [login, register] = document.getElementsByTagName('button')
var url = 'http://10.0.0.24:4444'

login.onclick = function () {
  fetch(url + '/login', {
    method: 'POST',
    body: JSON.stringify({
      username: username.value,
      password: password.value
    }),
    headers: {
      'content-type': 'application/json'
    }
  })
    .then(res => res.json())
    .then(data => {
      if (data.message === 'success') {
        window.location.href = './dashboard.html'
      } else {
        alert('登陆失败')
      }
    })
}

register.onclick = function () {
  fetch(url + '/register', {
    method: 'POST',
    body: JSON.stringify({
      username: username.value,
      password: password.value
    }),
    headers: {
      'content-type': 'application/json'
    }
  })
    .then(res => res.json())
    .then(data => {
      if (data.message === 'success') {
        alert('注册成功')
      } else {
        alert('注册失败')
      }
    })
}

login.onclick = function () {
  fetch(url + '/login', {
    method: 'POST',
    body: JSON.stringify({
      username: username.value,
      password: password.value
    }),
    headers: {
      'content-type': 'application/json'
    }
  })
    .then(res => res.json())
    .then(data => {
      if (data.message === 'success') {
        window.location.href = './dashboard.html'
      } else {
        alert('登陆失败')
      }
    })
}

register.onclick = function () {
  fetch(url + '/register', {
    method: 'POST',
    body: JSON.stringify({
      username: username.value,
      password: password.value
    }),
    headers: {
      'content-type': 'application/json'
    }
  })
    .then(res => res.json())
    .then(data => {
      if (data.message === 'success') {
        alert('注册成功')
      } else {
        alert('注册失败')
      }
    })
}